/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.chooser.model;

import java.util.HashMap;
import javax.swing.ImageIcon;
import offset.nodes.Constants;

/**
 *  Provides a static repository of node type icons
 */
public class TypeIconProvider {
    private static TypeIconProvider instance = null;
    private HashMap<String,ImageIcon> nodeTypeIcons = new HashMap<String,ImageIcon>();
    private HashMap<Integer,ImageIcon> propertyTypeIcons = new HashMap<Integer,ImageIcon>();
    
    /**
     * The singleton pattern
     * 
     * @return the singleton instance
     */
    public static synchronized TypeIconProvider getInstance() {
        if (instance == null) {
            System.out.println("TypeIconProvider: new instance");
            instance = new TypeIconProvider();
        }
        
        return instance;
    }
    
    /**
     * Add a node type icon
     * 
     * @param nodeType the node type name
     * @param icon the icon
     */
    public void addNodeTypeIcon(String nodeType, ImageIcon icon) {
        nodeTypeIcons.put(nodeType, icon);
    }
    
    /**
     * Add a property type icon
     * 
     * @param propertyType the property type
     * @param icon the icon
     */
    public void addPropertyTypeIcon(int propertyType, ImageIcon icon) {
        propertyTypeIcons.put(new Integer(propertyType), icon);
    }
    
    /**
     * Return a node type icon, if there is any.
     * 
     * @param nodeType the node type name
     * @return the icon
     */
    public ImageIcon getNodeTypeIcon(String nodeType) {
        ImageIcon result = nodeTypeIcons.get(nodeType);
        if (result == null) // try the default
            result = nodeTypeIcons.get(Constants.CHILD_DEFAULT_NODE_TYPE);
        
        return result;
    }
    
    /**
     * Return a property type icon.
     * 
     * @param propertyType the property type
     * @return the icon
     */
    public ImageIcon getPropertyTypeIcon(int propertyType) {
        ImageIcon result = propertyTypeIcons.get(new Integer(propertyType));
        if (result == null) // try the default
            result = propertyTypeIcons.get(new Integer(-1));
        
        return result;
    }
    
    /**
     * Return the size of the node type storage.
     * 
     * @return the size in images
     */
    public int getNodeTypeSize() {
        return nodeTypeIcons.size();
    }
}
